//	--------------------------------------------------------------------
//	Copyright(C) 2006,2007 Zhao Yukun. All rights reserved.
//	This file is a part of the Xeres system.
//	Xeres 2007
//	Contact : xeres.engine@gmail.com
//	$(reservedInfo)
//	$(reservedDeclaration)
//	--------------------------------------------------------------------
///	@file	<RenderPipeline.cpp>
///	@path	~/src/graphics
///	@date	2008/04/04
///	@desc	.

#include "config/config.h"

#include "lib/system/thread_debug.h"

#include "graphics/RenderPipeline.h"

namespace xeres {

	// ctor
	RenderPipeline::RenderPipeline( void )
		: m_wallpaper( NULL )
		, m_overlay( NULL )
		, m_reorderPipeline( false )
	{
	}

	// dtor
	RenderPipeline::~RenderPipeline( void )
	{
	}

	// initialize
	void RenderPipeline::Initialize( void )
	{
		RenderSet::Initialize();
	}

	// finalize
	void RenderPipeline::Finalize( void )
	{
		RenderSet::Finalize();

		if( m_wallpaper.IsValid() )
		{
			m_wallpaper->Finalize();
			m_wallpaper = NULL;
		}
		if( m_overlay.IsValid() )
		{
			m_overlay->Finalize();
			m_overlay = NULL;
		}

		m_mainPass = NULL;
		m_pipeline.clear();
	}

	// set wall paper
	void RenderPipeline::SetWallpaper( RefWeak< ICanvas > wallpaper )
	{
		m_wallpaper = wallpaper;
	}

	// set overlay
	void RenderPipeline::SetOverlay( RefWeak< ICanvas > overlay )
	{
		m_overlay = overlay;
	}

	// set main pass
	void RenderPipeline::SetMainPass( RefWeak< IPass > pass )
	{
		m_mainPass = pass;
	}

	// GetMainPass
	RefWeak<IPass> RenderPipeline::GetMainPass( void )
	{
		return m_mainPass;
	}

	// insert pass
	bool RenderPipeline::InsertPass( const WString& name , const WString& dep ,
		RenderSetId sets , RefWeak< IPass > pass )
	{
		return false;
	}

	// clear sub pass
	bool RenderPipeline::ClearSubPass( const WString& path )
	{
		return false;
	}

	// clear pipeline
	void RenderPipeline::Reset( void )
	{
		m_pipeline.clear();
		m_reorderPipeline = false;
	}

	// resolve passes
	void RenderPipeline::ResolvePasses( void )
	{

	}

	// render
	void RenderPipeline::Render( void )
	{
		TRACK_FUNCTION_SCOPE();
		RUNNING_ON_THREAD( _S("render") );

		// Pass by pass rendering pipeline:

		// Setup sub passes & construct dependency by stages.
		if( m_reorderPipeline )
		{
			ResolvePasses();
		}

		BeginScene();

		// Main pass.
		if( m_mainPass.IsValid() )
		{
			// For all sub passes.
			for( size_t i = 0 , total = m_pipeline.size() ; i < total ; ++i )
			{
				_Pass * pass = m_pipeline[i];
				pass->ipass->Begin( *this );
				{
					SetRenderPass( pass->ipass );
					RenderSet::Render( pass->iset );
				}
				pass->ipass->End( *this );
			}

			// begin main pass
			m_mainPass->Begin( *this );
			SetRenderPass( m_mainPass );

			// first we render wall paper
			if( m_wallpaper.IsValid() )
			{
				m_wallpaper->Render( *this );
				// clear depth information of wall paper
				ClearDepth( 1.0f );
			}

			// main batch
			RenderSet::Render( RenderSet_Scene );

			// at last we render overlay
			if( m_overlay.IsValid() )
			{
				m_overlay->Render( *this );
			}

			// end main pass
			m_mainPass->End( *this );
		}
		else
		{
			//TRACE_WARNING( _S("Main pass in render pipeline is not defined.") );
			ClearColorAndDepth( PackColor4(0,0,0,1) , 1.0f );

			// first we render wall paper
			if( m_wallpaper.IsValid() )
			{
				m_wallpaper->Render( *this );
			}

			// at last we render overlay
			if( m_overlay.IsValid() )
			{
				// clear depth information
				ClearDepth( 1.0f );

				m_overlay->Render( *this );
			}
		}

		EndScene();
	}
	
} // namespace xeres
